package com.smedi.ismedi.constructionproject.core.domain.service.impl;

import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.BidSectionConfigOrganization;
import com.smedi.ismedi.constructionproject.core.domain.repository.BidSectionConfigOrganizationRepository;
import com.smedi.ismedi.constructionproject.core.domain.service.BidSectionConfigOrganizationDomainService;
import com.smedi.ismedi.constructionproject.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.constructionproject.core.port.adapter.service.OrganizationRemoteApiService;
import com.smedi.ismedi.organization.api.dto.response.ProjectOrganizationResponse;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-6-22 10:49
 */
@Slf4j
@Service
@AllArgsConstructor
public class BidSectionConfigOrganizationDomainServiceImpl implements BidSectionConfigOrganizationDomainService {
    private final OrganizationRemoteApiService organizationRemoteApiService;

    private final BidSectionConfigOrganizationRepository bidSectionConfigOrganizationRepository;

    @Override
    public void validate(List<Long> projectOrganizations, Long projectId) {
        if (projectOrganizations.size() != new HashSet<>(projectOrganizations).size()) {
            throw LogicException.raise(CustomError.BID_SECTION_CONFIG_ORGANIZATION_DUPLICATE);
        }
        // 判断当前项目组织是否可用
        for (Long projectOrganizationId : projectOrganizations) {
            if (projectOrganizationId != null) {
                // 当前组织是否允许成为标段配置组织
                if (!organizationRemoteApiService.checkProjectOrganizationCanBeBidSectionConfig(projectOrganizationId)) {
                    throw LogicException.raise(CustomError.PROJECT_ORG_CAN_NOT_BE_CONFIG);
                }
                // 获取当前组织是否存在
                ProjectOrganizationResponse projectOrganization = organizationRemoteApiService.getProjectOrganizationView(projectOrganizationId);
                if (projectOrganization != null) {
                    if (!projectId.equals(projectOrganization.getProjectId())) {
                        throw LogicException.raise(CustomError.PROJECT_ORGANIZATION_NOT_BELONG_PROJECT);
                    }
                } else {
                    throw LogicException.raise(CustomError.ORG_NOT_AVAILABLE);
                }
            }
        }
    }

    @Override
    public void cleanByBidSectionId(Long bidSectionId) {
        bidSectionConfigOrganizationRepository.deleteByBidSectionId(bidSectionId);
    }

    @Override
    public List<BidSectionConfigOrganization> saveBidSectionConfigOrganization(List<BidSectionConfigOrganization> bidSectionConfigOrganizations, Long bidSectionId, Long projectId, Long bidSectionConfigId) {
        List<BidSectionConfigOrganization> results = new ArrayList<>();
        for (BidSectionConfigOrganization bidSectionConfigOrganization : bidSectionConfigOrganizations) {
            bidSectionConfigOrganization.setBidSectionConfigId(bidSectionConfigId)
                    .setBidSectionId(bidSectionId)
                    .setProjectId(projectId);
            results.add(bidSectionConfigOrganizationRepository.doInsert(bidSectionConfigOrganization));
        }

        return results;
    }
}
